package cn.gzjp.modules.gas.service;

import cn.gzjp.common.utils.pay.PayUtils;
import cn.gzjp.modules.it.scheduler.TaskJob;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 微信支付
 * @author jp_zh
 *
 */
@Service
public class WxMyPayService {

	/*
	 * 生成预付参数
	 */
	public String getPayInfo(String orderId, String body,BigDecimal totolMoney, String appid, String macid,
							 String openid, HttpServletRequest request,
							 String key, String notifiyUrl,String type) {
		String nonceStr = PayUtils.createNoncestr();
		String pirce = String.valueOf(totolMoney
				.multiply(new BigDecimal(100)).intValue());

		String ip = getIpAddr(request);
		String sign = "";
		String prepay_id = "";
		TreeMap<String, String> treeMap = new TreeMap<String, String>();
		treeMap.put("appid", appid);
		treeMap.put("mch_id", macid);
		treeMap.put("nonce_str", nonceStr);
		treeMap.put("body", body);
		treeMap.put("out_trade_no", orderId+"_"+System.currentTimeMillis());
		treeMap.put("total_fee", pirce);
		treeMap.put("spbill_create_ip", ip);
		treeMap.put("trade_type", "JSAPI");
		treeMap.put("openid", openid);
		treeMap.put("notify_url", notifiyUrl);
		prepay_id = PayUtils.getPrepayId(treeMap, key);
		String timeStamp = String.valueOf(System.currentTimeMillis() / 1000L);
		String packages = "prepay_id=" + prepay_id;
		String signType = "MD5";
		if (prepay_id == null) {
			System.out.println(orderId + "生成微信预付订单失败");
			return null;
		}
		TaskJob.prepayIdMap.put(orderId,prepay_id);
		SortedMap<String, String> finalpackage = new TreeMap<String, String>();
		finalpackage.put("appId", appid);
		finalpackage.put("timeStamp", timeStamp);
		finalpackage.put("nonceStr", nonceStr);
		finalpackage.put("package", packages);
		finalpackage.put("signType", "MD5");

		sign = PayUtils.getPaySign(finalpackage, key);
		sign = sign.toLowerCase();

		System.out.println("本地订单号：" + orderId + ",预支付订单号：" + prepay_id
				+ " ,金额为：" + pirce + "发起支付！");
		System.out.println("appId：" + appid + ",timeStamp：" + timeStamp + ",nonceStr："
				+ nonceStr + ",packages：" + packages + "，签名为:" + sign);
		JSONObject json = new JSONObject();
		json.put("appId", appid);
		json.put("timeStamp", timeStamp);
		json.put("nonceStr", nonceStr);
		json.put("package", packages);
		json.put("signType", signType);
		json.put("paySign", sign);
		return json.toString();
	}
	

	// 获取访问用户IP
	protected static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
